#include <mutex>
#include <functional>
#include <memory>
#include <condition_variable>

using namespace std;

class Foo
{
public:
    int flag = 0;
    condition_variable cv;
    mutex m;
    Foo()
    {
    }

    void first(function<void()> printFirst)
    {
        unique_lock<mutex> l(m);
        cv.wait(l, [this]()
                { return this->flag == 0; });
        flag++;
        printFirst();
        cv.notify_all();
    }

    void second(function<void()> printSecond)
    {
        unique_lock<mutex> l(m);
        cv.wait(l, [this]()
                { return this->flag == 1; });
        flag++;
        printSecond();
        cv.notify_all();
    }

    void third(function<void()> printThird)
    {

        unique_lock<mutex> l(m);
        cv.wait(l, [this]()
                { return this->flag == 2; });
        flag++;
        // printThird() outputs "third". Do not change or remove this line.
        printThird();
    }
};